<%
    ' Calcula los digitos verificadores horizontales
    Function calcularDVH(texto)
	    Dim i, total, dvh, ltexto
	    total = 0
	    ltexto=len(texto)
	    For i = 1 To ltexto
		    total = total + asc(mid(texto,i,1))
	    Next
	    dvh=total mod 10
	    calcularDVH = CInt(dvh)
    End Function

    ' Comprueba los digitos verificadores horizontales
    Function comprobarDVH()
        dim consulta
        dim NumReg
        consulta = "select * from DIGITOS_VERIFICADORES"
        set rs = ConsultarBDD(consulta,conexion)
        while not rs.EOF
            consulta = "select * from " & rs.fields(1)
            set rs2 = ConsultarBDD(consulta,conexion)
            NumReg=1
            while not rs2.EOF 
                dim tot,i
                tot=""
                For i = 0 To rs2.fields.count - 2
                    tot = tot & rs2.fields(i)
                Next
                If Not calcularDVH(tot) = rs2.fields("DVH") Then	
                    session("ErrorDVH")="<img border='0' alt='Error' title='Error' src='imagenes/error_small.png' width='16' height='16' />Error de DVH producido en la tabla " & rs.fields(1) & " en el registro numero " & NumReg & ".</br>"
                    comprobarDVH = 0
                    exit function
                else
                    comprobarDVH = 1
                End If
                NumReg=NumReg+1
                rs2.movenext 
            wend
        rs.movenext 
        wend
    End Function

    ' Recalcula los digitos verificadores horizontales
    Sub recalcularDVH()
	    Dim consulta
	    consulta = "SELECT * FROM DIGITOS_VERIFICADORES"
	    set rs = ConsultarBDD(consulta,conexion)
	    while not rs.EOF 
		    consulta = "SELECT * FROM " & rs.fields(1)
		    set rs2 = ConsultarBDD(consulta,conexion)
		    while not rs2.EOF 
        	    Dim tot, i, digito
			    tot = ""
                For i = 0 To rs2.fields.count - 2
            	    tot = tot & rs2.fields(i)
                Next
			    digito=0
			    digito = calcularDVH(tot)
			    if rs.fields(1) = "USUARIOS" then
				    consulta = "UPDATE " & rs.fields(1) & " SET DVH = " & digito & " WHERE id_Usuario = " & rs2.fields(0)
			    elseif rs.fields(1) = "PRODUCTOS" then
				    consulta = "UPDATE " & rs.fields(1) & " SET DVH = " & digito & " WHERE id_Producto = " & rs2.fields(0)
			    elseif rs.fields(1) = "BITACORA" then
				    consulta = "UPDATE " & rs.fields(1) & " SET DVH = " & digito & " WHERE id_Bitacora = " & rs2.fields(0)
			    end if
			    EjecutarConsulta consulta, conexion
			    rs2.movenext
		    wend
		    rs.movenext
	    wend
    End Sub

    ' Recalcula los digitos verificadores verticales
    Sub calcularDVV(tabla)
	    consulta = "select * from Digitos_Verificadores where Nombre_Tabla='" & tabla & "'"
	    set dscomprobar = ConsultarBDD(consulta,conexion)
	    If dscomprobar.EOF Then
		    consulta = "Select SUM(DVH) from " & tabla
		    set ds = ConsultarBDD(consulta,conexion)
		    consulta = "INSERT into DIGITOS_VERIFICADORES(Nombre_Tabla,DVV) values ('" & tabla & "'," & ds.fields(0) & ")"
		    EjecutarConsulta consulta,conexion
	    Else
		    consulta = "Select SUM(DVH) from " & tabla
		    set ds = ConsultarBDD(consulta,conexion)
		    consulta = "UPDATE DIGITOS_VERIFICADORES set DVV=" & ds.fields(0) & " where Nombre_Tabla='" & tabla & "'"
		    EjecutarConsulta consulta,conexion
	    End If
    End Sub

    ' Comprueba los digitos verificadores verticales
    Function comprobarDVV()
	    consulta = "SELECT * FROM DIGITOS_VERIFICADORES"
	    set rs = ConsultarBDD(consulta,conexion)
	    while not rs.EOF
		    Dim total
		    total=""
		    consulta = "select sum(DVH) from " & rs.fields(1)
		    set rs2 = ConsultarBDD(consulta,conexion)
		    If Not rs.fields(2) = rs2.fields(0) Then
			    session("ErrorDVV")="<img border='0' alt='Error' title='Error' src='imagenes/error_small.png' width='16' height='16' />Error de DVV producido en la tabla " & rs.fields(1) & ".</br>"
			    comprobarDVV = 0
			    exit function
		    else
			    comprobarDVV = 1
		    End If
		    rs.movenext
	    wend
    End Function

    ' Recalcular los digitos verificadores verticales
    Sub recalcularDVV()
	    consulta = "SELECT * FROM DIGITOS_VERIFICADORES"
	    set rs = ConsultarBDD(consulta,conexion)
	    while not rs.EOF
		    consulta = "SELECT SUM(DVH) FROM " & rs.fields(1)
		    set rs2 = ConsultarBDD(consulta,conexion)
		    consulta = "UPDATE DIGITOS_VERIFICADORES SET DVV = " & rs2.fields(0) & " WHERE NOMBRE_TABLA = '" & rs.fields(1) & "'"
		    EjecutarConsulta consulta, conexion
		    rs.movenext
	    wend
    End Sub

    ' Recalcula los digitos verificadores horizontales y verticales
    Sub recalcular_digitos()
        recalcularDVH()
        recalcularDVV()
    End Sub
%>